package com.information.manage.gyjc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.information.manage.basic.common.result.Result;
import com.information.manage.gyjc.mapper.ProductMapper;
import com.information.manage.gyjc.model.enity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/gyjc/product")
public class ProductController {
    @Autowired

    private ProductMapper productMapper;

    // 查询产品列表
    @GetMapping("/list/{curPage}/{size}")
    public Result selectProductList(@PathVariable Integer curPage, @PathVariable Integer size) {
        Page<Product> page = productMapper.selectPage(new Page<>(curPage, size), null);
        return Result.ok(page);
    }

    // 根据ID查询产品
    @GetMapping("/select/{id}")
    public Result<Product> selectProductById(@PathVariable Integer id) {
        Product product = productMapper.selectById(id);
        return Result.ok(product);
    }

    // 根据ID删除产品
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteProductById(@PathVariable Integer id) {
        int result = productMapper.deleteById(id);
        if (result > 0) {
            return Result.ok("删除成功");
        }
        return Result.fail("删除失败");
    }

    // 更新产品
    @PostMapping("/update")
    public Result<String> updateProductById(@RequestBody Product product) {
        int result = productMapper.updateById(product);
        if (result > 0) {
            return Result.ok("更新成功");
        }
        return Result.fail("更新失败");
    }

    // 添加产品
    @PostMapping("/add")
    public Result<String> addProduct(@RequestBody Product product) {
        int result = productMapper.insert(product);
        if (result > 0) {
            return Result.ok("添加成功");
        }
        return Result.fail("添加失败");
    }
}
